package com.jbinfo.common.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

	/**
	 * 检查字符串str是否匹配正则表达式regex
	 * 
	 * @param regex
	 *            正则表达式
	 * @param str
	 *            要检查的字符串
	 * @return boolean
	 */
	public static boolean matcherRegex(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	/**
	 * 检查电子邮件是否正确
	 * 
	 * @param email
	 *            电子邮件
	 * @return boolean 邮箱正确返回true，否则返回false
	 */
	public static boolean checkEmail(String email) {
		String regex = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		return matcherRegex(regex, email);
	}

	/**
	 * 转换编码到GBK
	 * 
	 * @param ContentStr
	 * @return
	 */
	public static String convertToGBK(String ContentStr, String charsetName) throws UnsupportedEncodingException {
		return new String(ContentStr.getBytes(charsetName), "GBK");
	}

	/**
	 * 判断字符串是否是数字（不为null，""或" "）
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotBlankNumeric(String str) {
		if (isNotBlank(str) && isNumeric(str)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符类型
	 * 
	 * @param ch
	 *            传入的字符
	 * @return int 隔断字符（空格、制表）返回0，数字返回1，字母返回2，其他返回3
	 */
	public static int tokenCharType(char ch) {
		if ((ch == ' ') || (ch == '\t')) {
			return 0;
		}
		if ((ch >= '0') && (ch <= '9')) {
			return 1;
		}
		if ((ch >= 'A') && (ch <= 'Z')) {
			return 2;
		}
		return 3;
	}

	public static String removeEnter(String str) {
		if (isNotEmpty(str)) {
			str = remove(str, "\r");
			str = remove(str, "\n");
		}
		return str;
	}

	/**
	 * 去掉终端号码的86前缀
	 * 
	 * @param termid
	 *            String
	 * @return String
	 */
	public static String trim86(String termid) {
		return termid.startsWith("86") ? termid.substring(2) : termid;
	}

	/**
	 * 将InputStream转成字符串
	 * 
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "/n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();

	}

	/**
	 * 将InputStream转成字符串
	 * 
	 * @param is
	 * @return
	 */
	public static String inputStream2String(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		return baos.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String cardId = "110223198207121895";
		System.out.println(substring(cardId, cardId.length() - 6));

	}

	/**
	 * int 型数字转换成字符串
	 * 
	 * @param i
	 * @return
	 */
	public static String intToString(int i) {
		return new Integer(i).toString();
	}

	public static int Str2Int(String s) {

		int ii = 0;

		if (s == null || s.equals(""))
			return 0;
		else {

			try {
				ii = Integer.parseInt(s);
			} catch (Exception e) {

			}
		}
		return ii;

	}

	/**
	 * 获取随机串
	 * 
	 * @param length
	 *            随机串长度
	 * @param type
	 *            类型 ：1. 数字 ；2.字母 ；3.数字+字母；
	 * @return
	 * @date 2013-7-10
	 */
	public static String getRandomString(int length, int type) { // length表示生成字符串的长度
		String string = "";
		switch (type) {
		case 1:// 数字
			string = getRandomString(length, "0123456789");
			break;
		case 2:// 字母
			string = getRandomString(length, "abcdefghijklmnopqrstuvwxyz");
			break;
		case 3:// 数字+字母
			string = getRandomString(length, "abcdefghijklmnopqrstuvwxyz0123456789");
			break;
		default:
			break;
		}
		return string;
	}

	/**
	 * 获取随机字符串
	 * 
	 * @param length
	 * @return
	 * @date 2013-7-10
	 */
	public static String getRandomString(int length, String baseString) { // length表示生成字符串的长度
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(baseString.length());
			sb.append(baseString.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 字符串首字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String toLowerCaseFirstOne(String str) {
		if (str == null || "".equals(str))
			return str;
		if (Character.isLowerCase(str.charAt(0)))
			return str;
		else
			return (new StringBuilder()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
	}

	/**
	 * 字符串首字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String toUpperCaseFirstOne(String str) {
		if (str == null || "".equals(str))
			return str;
		if (Character.isUpperCase(str.charAt(0)))
			return str;
		else
			return (new StringBuilder()).append(Character.toUpperCase(str.charAt(0))).append(str.substring(1)).toString();
	}
}
